package com.example.tetris.beans;

import com.example.tetris.ResourceTable;
import com.example.tetris.beans.RecordingTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.List;

/**
 * 适配器的作用：将集合中的数据List绑定到一个item的布局中
 * 1.继承BaseItemProvider，实现四个方法
 * 2.定义一个数据集合，通过有参构造器传入集合
 * 3.定义一个AbilitySlice
 */
public class RecordingProvider extends BaseItemProvider {

    private List<RecordingTable> list;
    private AbilitySlice abilitySlice;

    public RecordingProvider(List<RecordingTable> list, AbilitySlice abilitySlice) {
        this.list = list;
        this.abilitySlice = abilitySlice;
    }

    /**用于渲染列表中的数据的：集合中有多少条数据，此方法被调用多少次
     *
     *
     * @param i 表示调用的索引
     * @param component
     * @param componentContainer
     * @return
     */
    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        //每次获取一条数据（数据源）
        RecordingTable recording = list.get(i);
        //获取item模板,需要视图容器上下文Ability/AbilitySlice
        DirectionalLayout itemlayout = (DirectionalLayout) LayoutScatter.getInstance(abilitySlice)
                .parse(ResourceTable.Layout_recording_item,null,false);
        Text date = (Text) itemlayout.findComponentById(ResourceTable.Id_game_date);
        Text score = (Text) itemlayout.findComponentById(ResourceTable.Id_game_score);
        Text seconds = (Text) itemlayout.findComponentById(ResourceTable.Id_game_seconds);
        //绑定数据
        date.setText(recording.getDate());
        score.setText(String.valueOf(recording.getScore()));
        seconds.setText(String.valueOf(recording.getSeconds()));
        //返回渲染数据后的组件
        return itemlayout;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

}
